package com.hsd.znsh.controller.root;

import com.hsd.znsh.entity.Role;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.service.RoleService;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-09 13:12:01
 */
@RequestMapping("/root/role")
@Controller
public class RootRoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/roleList")
    public String toRoleList(){
        return "manager/function/roleList";
    }

    @GetMapping("/edit")
    public String toEdit(){
        return "manager/function/roleEdit";
    }

    @GetMapping("/addRole")
    public String toAddRole(){
        return "manager/function/roleAdd";
    }

    @PostMapping("/addRole")
    @ResponseBody
    public ResultVO addRole(@Valid Role role, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        roleService.addRole(role);
        return ResultVOUtil.success("添加成功");
    }

    @PostMapping("/delete/{roleId}")
    @ResponseBody
    public ResultVO addRole(@PathVariable("roleId") Integer roleId){
        roleService.deleteRole(roleId);
        return ResultVOUtil.success("添加成功");
    }

    @GetMapping("/getRoleList")
    @ResponseBody
    public ResultVO getRoleListTotalPages(Role role, Integer size){
        long totalNum=roleService.getRoleListTotalPages(role);
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getRoleList")
    @ResponseBody
    public ResultVO getRoleList(Role role,@PageableDefault(sort = "id") Pageable pageable){
        List<Role> list=roleService.getRoleList(role,pageable);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getFunctionTree")
    @ResponseBody
    public List<Map> getFunctionTree(){
        List<Map> list=roleService.getFunctionTree();;
        return list;
    }

    @PostMapping("/assignRoleFunction")
    @ResponseBody
    public ResultVO assignRoleFunction(String functionIds,Integer roleId){
        roleService.assignRoleFunction(functionIds,roleId);
        return ResultVOUtil.success();
    }
}
